home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 05.zip / BS1 part 5 / SASC_6.0_Disk_7.adf / Source_And_Examples / examples / cback / back.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-30  |  5.7 KB  |  193 lines

  1. /*
  2.     SAS/C Background Example
  3.     version 6.0   1992
  4.  
  5.     This is a program which demonstrates how to write a simple background
  6.     process.  This program opens a window and writes a message.  You may close
  7.     the CLI it was run from before closing the window.
  8.  
  9.     You should compile this program with the following options:
  10.  
  11.                STRUCTUREEQUIVALENCE
  12.                LINK
  13.                STARTUP=cback
  14.                NOSTANDARDIO
  15.  
  16.  
  17.     STRUCTUREEQUIVALENCE is not required, but it does circumvent several
  18.     warnings that would otherwise be produced.
  19.  
  20.     STARTUP=cback tells slink to link with the background startup code.  This
  21.     is required to produce a background process.  If you link in a separate step,
  22.     you must link with cback.o instead of c.o.
  23.  
  24.     NOSTANDARDIO is required to keep the startup code from opening stderr, stdin,
  25.     and stdout.  If any of these are opened, then the you cannot close the
  26.     CLI window from which the background process was opened.  
  27.     
  28.     *** IMPORTANT **  You must remember when writing a backgorund process not to 
  29.     call printf() or other standard IO functions.  If you do, you will probably
  30.     crash the machine.
  31.  
  32.     You may notice that this program does not open any AmigaDOS libraries.  Instead
  33.     it takes advantage of the fact that the version 6.0 compiler now opens
  34.     needed AmigaDOS libraries for you.
  35.      
  36. */
  37.  
  38. /**************************  INCLUDES  ************************/
  39. #include <exec/types.h>
  40. #include <exec/execbase.h>
  41. #include <intuition/intuition.h>
  42. #include <proto/intuition.h>
  43. #include <proto/exec.h>
  44. #include <proto/dos.h>
  45. #include <dos/dos.h>
  46. #include <string.h>
  47. #include <stdlib.h>
  48.  
  49.  
  50. /**************************  CONSTANTS    ***********************/
  51. #define BANNER "\x9B""0;33mSAS/C Background Example\x9B""0m \nCopyright \xA9 1992 SAS Institute, Inc.\n"
  52.  
  53.  
  54. /**************************  PROTOTYPES  **********************/
  55. void clean_exit(struct Window *, struct IOStdReq *, int);
  56. void action(void);
  57.  
  58.  
  59. /**********************  CBACK DECLARATIONS  ******************/
  60. long __stack = 4000;            /* Amount of stack space our task needs */
  61. char *__procname = "SAS/C Background";  /* The name of the task to create       */
  62. long __priority = 0;            /* The priority to run the task at    */
  63. long __BackGroundIO = 1;      /* Flag indicating we want to send I/O to the
  64.                      original shell.  We will print a banner.
  65.                      NOTE:  This variable may also be called
  66.                      _BackGroundIO.  Notice the single
  67.                      underscore.                 */
  68. extern BPTR _Backstdout;      /* File handle pointing to originating shell
  69.                    (standard output when run in background) */
  70.  
  71. /********************************************************************/
  72.  
  73. extern struct ExecBase *SysBase;
  74.  
  75. void main (void)
  76. {
  77.    /*  Write a copyright banner */
  78.    if (_Backstdout)
  79.       {
  80.       Write(_Backstdout, BANNER, sizeof(BANNER));
  81.       Close(_Backstdout);
  82.       }
  83.  
  84.    /*  Call a function performing some action.    In this case the function will
  85.        open a window and wait for the user to close it.  */
  86.    action();
  87.  
  88.    clean_exit(NULL, NULL, NULL);
  89. }
  90.  
  91. /********************************************************************/
  92.  
  93. /*  Clean up and exit */
  94. void clean_exit(struct Window * w, struct IOStdReq *writereq, int closedev)
  95. {
  96.    if (closedev)
  97.       CloseDevice(writereq);
  98.  
  99.    if (w)
  100.        CloseWindow(w);
  101.  
  102.    exit(0);
  103. }
  104.  
  105.  
  106. /********************************************************************/
  107.  
  108.  
  109. /*  This function will open an Intuition window, attach a console device to it,
  110.     and then write to the window.  The function will then wait until the user
  111.     clicks on the close gadget of the window before closing the window and
  112.     exiting the program.    */
  113.  
  114. void action(void)
  115. {
  116.     struct Window *w;
  117.  
  118.     struct NewWindow nw = {
  119.         100,100,                  /* Starting corner */
  120.         292,100,                  /* Width, height */
  121.         2,1,                      /* Detail, Block pens */
  122.         CLOSEWINDOW | NEWSIZE,    /* IDCMP flags */
  123.         WINDOWDEPTH | WINDOWDRAG | WINDOWCLOSE | WINDOWSIZING,
  124.                               /* Window flags */
  125.         NULL,                      /* Pointer to first gadget */
  126.         NULL,                      /* Pointer to checkmark */
  127.         "SAS/C Background Example",       /* Title */
  128.         NULL,                      /* Screen pointer */
  129.         NULL,                      /* Bitmap pointer */
  130.         100,100,(USHORT)~0,(USHORT)~0,  /* Allow window to open to
  131.                        maximum screen size    */
  132.         WBENCHSCREEN              /* Type of screen */
  133.         };
  134.  
  135.     struct IntuiMessage * msg = NULL;
  136.  
  137.     struct IOStdReq *writereq;
  138.  
  139.     int type;
  140.     WORD Class;
  141.     int done = 0;
  142.     BYTE error;
  143.     char *string = "You may close the CLI window now and this window will remain.";
  144.  
  145.     /*  Set type of window opened based on AmigaDOS version number */
  146.     if(SysBase->LibNode.lib_Version < 37)
  147.        {
  148.        type = 0;
  149.        nw.Flags |= SMART_REFRESH;
  150.        }
  151.     else 
  152.        {
  153.        type = 3;   
  154.        nw.Flags |= SIMPLE_REFRESH;
  155.        }
  156.  
  157.     /*    Open the window.   */
  158.     w = OpenWindow(&nw);
  159.     if(w  == NULL)
  160.        clean_exit(w, NULL, 0);
  161.  
  162.     writereq = calloc(1, sizeof(struct IOStdReq));
  163.  
  164.     /*    First open the console device, then write a message to the window.  */
  165.     writereq->io_Data = (APTR) w;
  166.     writereq->io_Length = sizeof(struct Window);
  167.     error = OpenDevice("console.device", type, writereq, 0);
  168.     if (error != NULL)
  169.        clean_exit(w, writereq, 0);
  170.  
  171.     writereq->io_Command = CMD_WRITE;
  172.     writereq->io_Data = (APTR) string;
  173.     writereq->io_Length = strlen(string);
  174.     DoIO(writereq);
  175.  
  176.     /*    Wait for a signal.  */
  177.     while ( !done )
  178.     {
  179.        WaitPort(w->UserPort);
  180.        while(msg = (struct IntuiMessage *) GetMsg(w->UserPort))
  181.        {
  182.           /*  When a signal is received, reply to it and evaluate it  */
  183.           Class = msg->Class;
  184.           ReplyMsg ( (struct Message *) msg);
  185.           if (Class == CLOSEWINDOW)
  186.             done = 1;
  187.        }
  188.     }
  189.  
  190.     clean_exit(w, writereq, 1);
  191. }
  192.  
  193.